package com.clz.appdesign.myapp.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.clz.appdesign.myapp.view.entiity.Pie;

import java.util.ArrayList;

/**
 * @author chenliangzhi
 * @date 2018/5/31
 * @describe
 */

public class PieView extends View {
    //颜色表
    private int[] mColors = {0xFFCCFF00, 0xFF6495ED, 0xFFE32636, 0xFF800000, 0xFF808000, 0xFFFF8C69, 0xFF808080,
            0xFFE6B800, 0xFF7CFC00};
    //饼状图初始绘制角度
    private int mStartAngle;
    //扇形图的数据
    private ArrayList<Pie> mData;
    //宽高
    private int mWidth, mHeight;
    //
    private Paint mPaint = new Paint();

    public PieView(Context context) {
        super(context);
        //初始化画笔
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
    }

    public PieView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //初始化画笔
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
    }

    public PieView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mData == null) {
            return;
        }
        //当前起始角度
        float currentStartAngle = mStartAngle;
        //将画布坐标原点移到中心位置
        canvas.translate(mWidth / 2, mHeight / 2);
        //饼状图半径
        float r = (float) (Math.min(mWidth, mHeight) / 2 * 0.8);
        RectF rectF = new RectF(-r, -r, r, r);

        for (int i = 0; i < mData.size(); i++) {
            Pie pie = mData.get(i);
            mPaint.setColor(pie.getColor());
            canvas.drawArc(rectF, currentStartAngle, pie.getAngle(), true, mPaint);
            currentStartAngle += pie.getAngle();
        }
    }

    //设置起始角度
    public void setStartAngle(int angle) {
        this.mStartAngle = angle;
        invalidate();
    }

    //设置数据
    public void setData(ArrayList<Pie> data) {
        this.mData = data;
        initData(data);
        invalidate();
    }

    private void initData(ArrayList<Pie> data) {
        if (data == null) {
            return;
        }
        float sumValue = 0;
        for (int i = 0; i < data.size(); i++) {
            final Pie pie = data.get(i);
            sumValue += pie.getValue();
            int j = i % mColors.length;
            mPaint.setColor(mColors[j]);
        }

        float sumAngle=0;
        for (int i = 0; i < data.size(); i++) {
            final Pie pie = data.get(i);
            float pecentAngle=pie.getValue()/sumValue;
            float angle=pecentAngle*360;
            pie.setPercent(pecentAngle);
            pie.setAngle(angle);
            sumAngle+=angle;
        }

    }

}
